<?php

namespace app\common\model\option;

use app\common\model\order\Order;
use app\common\model\red\Log;
use app\common\model\User;
use think\Model;


class Option extends Model
{





    // 表名
    protected $name = 'option';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];










    public function admin()
    {
        return $this->belongsTo('app\common\model\Admin', 'id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public static function market()
    {
        $freight = Order::whereTime('paytime', 'yesterday')->where('status', '>', 0)->sum('freight');
        $payMoney = Order::whereTime('paytime', 'yesterday')->where('status', '>', 0)->sum('pay_money');
        $where['invalidtime'] = ['>', time()];
        $where['status'] = ['=', 'normal'];
        $where['state'] = ['=', '1'];
        $option = User::sum('options');
        $total = bcsub($payMoney, $freight ?? 0, 2);
        return $option?bcdiv(bcmul($total, 0.1, 2), $option, 2):$option;
    }
}
